require("mason").setup()
require("mason-lspconfig").setup {
  ensure_installed = { "bashls", "lua_ls", "rust_analyzer", "clangd", "ts_ls", "pyright", "vimls" },
}


local lspconfig = require('lspconfig')
-- Use LspAttach autocommand to only map the following keys
-- after the language server attaches to the current buffer
vim.api.nvim_create_autocmd('LspAttach', {
  group = vim.api.nvim_create_augroup('UserLspConfig', {}),
  callback = function(ev)
    -- Enable completion triggered by <c-x><c-o>
    vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc'

    -- Buffer local mappings.
    -- See `:help vim.lsp.*` for documentation on any of the below functions
    local opts = { buffer = ev.buf }
    vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, opts)
    vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
    vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
    vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts)
    vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts)
    vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, opts)
    vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, opts)
    vim.keymap.set('n', '<space>wl', function()
      print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
    end, opts)
    vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, opts)
    vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts)
    vim.keymap.set({ 'n', 'v' }, '<space>ca', vim.lsp.buf.code_action, opts)
    -- vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
    vim.keymap.set('n', '<space>f', function()
      vim.lsp.buf.format { async = true }
    end, opts)
  end,
})
local icons = {
  Array = "",
  Boolean = "",
  Class = "",
  Color = "",
  Constant = "",
  Constructor = "",
  Enum = "",
  EnumMember = "",
  Event = "",
  Field = "",
  File = "",
  Folder = "󰉋",
  Function = "",
  Interface = "",
  Key = "",
  Keyword = "",
  Method = "",
  Module = "",
  Namespace = "",
  Null = "󰟢",
  Number = "",
  Object = "",
  Operator = "",
  Package = "",
  Property = "",
  Reference = "",
  Snippet = "",
  String = "",
  Struct = "",
  Text = "",
  TypeParameter = "",
  Unit = "",
  Value = "",
  Variable = "",
}
local options = {
  icons = {
    Array = icons.Array .. " ",
    Boolean = icons.Boolean .. " ",
    Class = icons.Class .. " ",
    Color = icons.Color .. " ",
    Constant = icons.Constant .. " ",
    Constructor = icons.Constructor .. " ",
    Enum = icons.Enum .. " ",
    EnumMember = icons.EnumMember .. " ",
    Event = icons.Event .. " ",
    Field = icons.Field .. " ",
    File = icons.File .. " ",
    Folder = icons.Folder .. " ",
    Function = icons.Function .. " ",
    Interface = icons.Interface .. " ",
    Key = icons.Key .. " ",
    Keyword = icons.Keyword .. " ",
    Method = icons.Method .. " ",
    Module = icons.Module .. " ",
    Namespace = icons.Namespace .. " ",
    Null = icons.Null .. " ",
    Number = icons.Number .. " ",
    Object = icons.Object .. " ",
    Operator = icons.Operator .. " ",
    Package = icons.Package .. " ",
    Property = icons.Property .. " ",
    Reference = icons.Reference .. " ",
    Snippet = icons.Snippet .. " ",
    String = icons.String .. " ",
    Struct = icons.Struct .. " ",
    Text = icons.Text .. " ",
    TypeParameter = icons.TypeParameter .. " ",
    Unit = icons.Unit .. " ",
    Value = icons.Value .. " ",
    Variable = icons.Variable .. " ",
  },
  lsp = {
    auto_attach = true,
  },
  highlight = true,
  -- separator = " " .. lvim.icons.ui.ChevronRight .. " ",
  depth_limit = 0,
  depth_limit_indicator = "..",
}
local ok, navic = pcall(require, "nvim-navic")
if ok then
  navic.setup (options)
end
-- require'lspconfig'.ltex.setup{}
--lspconfig.dartls.setup {}
--
require("flutter-tools").setup {} -- use defaults
lspconfig.marksman.setup {}
lspconfig.bashls.setup {}
lspconfig.lua_ls.setup {}
lspconfig.clangd.setup {}
lspconfig.rust_analyzer.setup {}
lspconfig.pyright.setup {}
lspconfig.gopls.setup {}
lspconfig.vimls.setup {}
lspconfig.ts_ls.setup {}
lspconfig.jsonls.setup {}
lspconfig.jdtls.setup({
  cmd = { "jdtls", "-languageserver" }
})
local util = require "lspconfig.util"
local server_config = {
  filetypes = { "c", "cpp", "objc", "objcpp", "opencl" },
  root_dir = function(fname)
    return util.root_pattern("compile_commands.json", "compile_flags.txt", ".git")(fname)
    or util.find_git_ancestor(fname)
  end,
  init_options = { cache = {
    -- or
   directory =  vim.fs.normalize "~/.cache/ccls" -- if on nvim 0.8 or higher
  } },
  --on_attach = require("my.attach").func,
  --capabilities = my_caps_table_or_func
}
-- require("ccls").setup { lsp = { lspconfig = server_config } }
